clear all

//------------------------------------------------------------------------------------//
//                Load and Clean Bank Location Data                                   //
//------------------------------------------------------------------------------------//

/* Import dataset containing department information for CIB-guichet */
import delimited "$input\CIB_guichet_departement.csv", clear
save "$output\CIB_guichet_departemens.dta", replace

/* Start logging */
capture log close
log using "$logs\trend_relative_visual.log", replace

//------------------------------------------------------------------------------------//
//                Load and Process Firm-Bank Dataset                                //
//------------------------------------------------------------------------------------//

/* Load dataset containing SIREN - CIB data */
use "$output\SCR_CIB_3_months_orbis_scrapped.dta", clear

/* Keep data between January 2014 and December 2019 */
keep if mdate < mofd(date("20200101","YMD"))
keep if mdate > mofd(date("20131231","YMD"))

/* Reclassify industries */
/* Pool P, Q, R, and S together as "public sector" */
replace industry="public" if inlist(industry, "P", "Q", "R", "S")

/* Merge industries D and E into "DE" */
replace industry="DE" if inlist(industry, "D", "E")

/* Merge industries F and L into "FL" */
replace industry="FL" if inlist(industry, "F", "L")

/* Remove agriculture (A), finance & insurance (K), public administration (O), 
   industry B, DE (due to lack of firms), and other unspecified industries */
drop if inlist(industry, "A", "K", "O", "B", "DE", "0000Z")

/* Convert Corsican department codes "2A" and "2B" to "2" */
replace dept="2" if inlist(dept, "2A", "2B")

/* Convert department codes to numeric format */
destring dept, gen(deptfirm) force

/* Merge department data with SIREN - CIB dataset */
merge m:1 cib guichet using "$output\CIB_guichet_departemens.dta"
keep if _merge == 3
drop _merge

//------------------------------------------------------------------------------------//
//                Compute Trends for FinTech Borrowers (Figure 1)                     //
//------------------------------------------------------------------------------------//

preserve
/* Keep only FinTech borrowers (p2p == 1) */
keep if p2p == 1

/* Remove outside loan */
keep if newoutside == 0

/* Keep relevant time window: -12 months to +24 months from event */
keep if inrange(deltamonths, -12, 24)

/* Compute loan variables */
gen loan = stloan + ltloan + bm + bi + oc
gen leasing = bm + bi

/* Aggregate loan components at firm-month level */
sort siren mdate cib
collapse (sum) loan available drawn stloan ltloan leasing oc co bm bi, by(siren p2p deltamonths year mdate)

/* Compute log-transformed loan variables */
gen lloan = log(loan)
gen ldrawn = log(loan - oc)
gen lavailable = log(oc)
gen lstloan = log(stloan)
gen lltloan = log(ltloan + bm + bi)
gen lleasing = log(leasing)
gen lco = log(co)
gen lother = log(loan - co - ltloan - leasing)

/* Transform deltamonths into non-negative categorical variable */
gen delta = deltamonths + 12

/* Define value labels for delta */
capture lab_delta drop 
forvalues n = 0(1)36 {
	local displayed_value = `n' - 12
	label define lab_delta `n'  "`displayed_value'", add
}
label val delta lab_delta

/* Regression and visualization */
foreach var in lloan lstloan lltloan ldrawn lavailable lleasing lco lother {
	eststo `var' : reg `var' ib11.delta, cluster(siren)
	coefplot (`var', ci(95) color(emerald) msize(medlarge) recast(line) lwidth(medlarge) ///
	ciopts(recast(rconnected) msymbol(smx) color(emerald) lwidth(thin) lpattern(dash))), ///
	omitted base keep(*.delta) rename(*.delta#*= "") vertical yline(0, lpattern(shortdash) lcolor(grey%40)) ///
	xlabel(2 "-11" 4 "-9" 6 "-7" 8 "-5" 10 "-3" 12 "-1" 14 "1" 16 "3" 18 "5" 20 "7" 22 "9" 24 "11" 26 "13" 28 "15" 30 "17" 32 "19" 34 "21" 36 "23", labsize(small)) xsize(6) ysize(4) ylabel(,nogrid) ///
	plotregion(fcolor(white) lcolor(white)) xline(12, lcolor(grey%40)) graphregion(color(white)) ///
	ytitle("")
	
	graph save "$figures\Figure_1_reg_coef_P2P_1_`var'_unmatched.gph", replace
	graph export "$figures\Figure_1_reg_coef_P2P_1_`var'_unmatched.pdf", replace
}
restore

//------------------------------------------------------------------------------------//
//                Compute Trends for Bank Borrowers                                   //
//------------------------------------------------------------------------------------//

preserve
/* Keep only bank borrowers (p2p == 0) */
keep if p2p == 0

/* Remove outside loan */
keep if newoutside == 0

/* Keep relevant time window: -12 months to +24 months from event */
keep if inrange(deltamonths, -12, 24)

gen loan = stloan + ltloan + bm + bi + oc
gen leasing = bm + bi

/* Aggregate loan components at firm-month level */
sort siren mdate cib
collapse (sum) loan available drawn stloan ltloan leasing oc co bm bi, by(siren p2p deltamonths year mdate)

/* Compute log-transformed loan variables */
gen lloan = log(loan)
gen ldrawn = log(loan - oc)
gen lavailable = log(oc)
gen lstloan = log(stloan)
gen lltloan = log(ltloan + bm + bi)
gen lleasing = log(leasing)
gen lco = log(co)
gen lother = log(loan - co - ltloan - leasing)

/* Transform deltamonths into non-negative categorical variable */
gen delta = deltamonths + 12

/* Define value labels for delta */
capture lab_delta drop 
forvalues n = 0(1)36 {
	local displayed_value = `n' - 12
	label define lab_delta `n'  "`displayed_value'", add
}
label val delta lab_delta

/* Regression and visualization */
foreach var in lloan lstloan lltloan ldrawn lavailable lleasing lco lother {
	qui eststo `var' : reg `var' ib11.delta, cluster(siren)
	coefplot (`var', ci(95) color(emerald) msize(medlarge) recast(line) lwidth(medlarge) ///
	ciopts(recast(rconnected) msymbol(smx) color(emerald) lwidth(thin) lpattern(dash))), ///
	omitted base keep(*.delta) rename(*.delta#*= "") vertical yline(0, lpattern(shortdash) lcolor(grey%40)) ///
	xlabel(2 "-11" 4 "-9" 6 "-7" 8 "-5" 10 "-3" 12 "-1" 14 "1" 16 "3" 18 "5" 20 "7" 22 "9" 24 "11" 26 "13" 28 "15" 30 "17" 32 "19" 34 "21" 36 "23", labsize(small)) xsize(6) ysize(4) ylabel(,nogrid) ///
	plotregion(fcolor(white) lcolor(white)) xline(12, lcolor(grey%40)) graphregion(color(white)) ///
	ytitle("")
	
	graph save "$figures\reg_coef_P2P_0_`var'_unmatched.gph", replace
	graph export "$figures\reg_coef_P2P_0_`var'_unmatched.pdf", replace
}
restore

********************************************************************************
* Comparison post-matching (Matched Sample Analysis) - Figure C1a
********************************************************************************

clear all

/* Use matched sample */
use "$output\matched_sample_nn.dta", clear

/* Sort data by firm and date */
sort siren mdate

/* Keep observations within the analysis period */
keep if mdate < mofd(date("20200101","YMD"))
keep if mdate > mofd(date("20131231","YMD"))

/* Count distinct firms by delta months */
preserve
keep if inrange(deltamonths, -12, 24)
bysort deltamonths: distinct siren
restore

/* Apply frequency weights */
keep if weight != .
su weight, d
gen max_weight = `r(max)'
expand max_weight
bys siren mdate: gen nb = _n 
keep if nb <= weight

/*********** Trend Analysis for Matched Sample *******************/

/* Compute credit trends post-matching */
preserve
keep if inrange(deltamonths, -12, 24)

/* Compute loan variables */
gen loan = stloan + ltloan + bm + bi + oc
collapse (sum) loan ltloan stloan co bm bi, by(siren p2p nb deltamonths year mdate)

/* Compute log-transformed loan variables */
gen lloan = log(loan)
gen lltloan = log(ltloan + bm + bi)
gen lstloan = log(stloan)
gen lco = log(co)
gen lother = log(loan - co - ltloan - bm - bi)

/* Compute mean and standard deviation for visualization */
collapse (mean) lloan lltloan lstloan lco lother /// 
(sd) sd_lloan=lloan sd_lltloan=lltloan sd_lstloan=lstloan sd_lco=lco sd_lother=lother /// 
(count) N = lloan, by(p2p deltamonths)

/* Create time shift variable to avoid overlap in the plot */
gen deltamonths2 = deltamonths + 0.25

/* Generate trend plot comparing FinTech and Bank borrowers */
foreach var in lloan {
    twoway (connected `var' deltamonths if p2p == 1, lpattern(dash) color(emerald) msize(medsmall) lpattern(shortdash) lwidth(medthick))  ///
           (connected `var' deltamonths2 if p2p == 0, color(gs10%80) msize(medsmall) msymbol(square) lpattern(dash) lwidth(medthick)), ///
    legend(label(1 "FinTech borrowers") label(2 "Bank borrowers") region(color(white)) size(medlarge) position(6)) ///
    plotregion(color(white)) graphregion(color(white)) ///
    xlabel(-12(2)24, labsize(small)) xtitle("") ///
    ytitle("Bank credit (excluding outside loan)") ///
    xsize(6) ysize(4) ylabel(, nogrid) xline(0) ///
    yscale(range(5 5.3)) ylab(5(0.05)5.3)

    /* Save graphs */
    graph save "$figures/Figure_C1a_Trend_relative_`var'_matched.gph", replace
    graph export "$figures/Figure_C1a_Trend_relative_`var'_matched.pdf", replace
}
restore

/* Close log file */
log close
